package cn.net.withub.dataCollector;

import cn.net.withub.dataCollector.common.model.TCollectorData;
import cn.net.withub.dataCollector.dao.impl.JdbcTool;
import cn.net.withub.dataCollector.web.service.CollectorService;
import cn.net.withub.dataCollector.web.service.impl.CollectorServiceImpl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;

/**
 *
 */
@RunWith(SpringJUnit4ClassRunner.class)//表示整合JUnit4进行测试
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})//加载spring配置文件
@WebAppConfiguration(value = "src/main/webapp")//设置web项目的环境，如果是Web项目，必须配置该属性，否则无法获取 web 容器相关的信息（request、context 等信息）
public class SaveImage {
    @Autowired
    private CollectorService service;

    @Autowired
    private JdbcTool jdbcTool;

    @Autowired
    private CollectorServiceImpl serviceImpl;

    private Logger log = Logger.getLogger(this.getClass());

    @Test
    public void test1() {
        service.saveImg();


    }

    @Test
    public void test2() {
        String id = "000020471-574d-4ca6-8cd9-152c568502d1";
        TCollectorData t1 = (TCollectorData) service.load(TCollectorData.class, " id = '" + id + "'");
        log.info(t1.getConfigId());
    }
    @Test
    public void updateContent() {
        service.updataContent();
    }

    @Test
    public void TestLocalSave() {
        serviceImpl.saveTest();
    }


    /**
     * 测试递归
     */
    @Test
    public void testDg1(){
        String sql = "SELECT * FROM `t_center_report_col_top_def` WHERE report_id=1";
        List<Map<String, Object>> list = jdbcTool.queryForList(sql);
        Map<String, List<Map<String, Object>>> treeMap = new HashMap<String, List<Map<String, Object>>>();
        log.info(list);
        String pid = "";
        List<Map<String, Object>> list_tmp=null;
        List<Map<String, Object>> allList = new ArrayList<Map<String, Object>>();
        List<Map<String, Object>> chidrenList = null;
        for (Map<String, Object> e : list) {
            pid = e.get("PARENT_DISPLAY_COL_ID") == null ? "0" : e.get("PARENT_DISPLAY_COL_ID").toString();
            if (treeMap.get(pid) != null) {
                treeMap.get(pid).add(e);
            } else {
                list_tmp = new ArrayList<Map<String, Object>>();
                list_tmp.add(e);
                treeMap.put(pid, list_tmp);
            }
        }
        log.info(treeMap);
        /*
		  这个pid的值根据需要来，
		  如果要获取整棵树，这里就填根节点的id，treeTbl表中的根节点的 id 为 0
		  如果是获取指定父级节点下的所有子孙节点，就填该父节点的 id 值
		 */
        chidrenList = treeMap.get("0");
        log.info(chidrenList);
        /*
		   上面得到的chidrenList仅仅是底下一层的节点集合，也就是说只包含了子节点，未包含子节点下的所有子孙节点
		   如果变量pid表示第一层的话，这里我们从第二层开始递归
		   也就是不停的寻找下一层下一层下一层……直到不存在下一层为止
		   如何判断有无下一层呢，之前创建好的HashMap已存好父子关系，用它来判断就好
		*/
        for (Map<String, Object> e : chidrenList) {
            getChildrens(treeMap, e);
        }
        //将之前第一层子节点也加入进来(这个地方根据需要来)
        allList.addAll(chidrenList);
        log.info(allList);
    }

    void getChildrens(Map<String, List<Map<String, Object>>> treeMap, Map<String, Object> map) {
        String id = map.get("DISPLAY_COL_ID").toString();
        //查询当前id下的子级的集合
        List<Map<String, Object>> chidrenList = treeMap.get(id);
        //添加子项 子节点
        map.put("chidrens", chidrenList);
        //plist.add(tempMap);
        if (chidrenList != null && chidrenList.size() > 0) {
            for (Map<String, Object> e : chidrenList) {
                //再查一次
                getChildrens(treeMap,e);
            }
        }
    }
    @Test
    public void testDg2(){
        String sql = "SELECT * FROM `t_center_report_col_top_def` WHERE report_id=1";
        List<Map<String, Object>> list = jdbcTool.queryForList(sql);
        log.info(list);
        String pid = "";
        List<Map<String, Object>> allList = new ArrayList<Map<String, Object>>();
        for (Map<String, Object> e : list) {
            pid = e.get("PARENT_DISPLAY_COL_ID") == null ? "0" : e.get("PARENT_DISPLAY_COL_ID").toString();
            if (pid.equals("0")) {
                allList.add(e);
            }
        }
        for (Map<String, Object> e : allList) {
            getChildrens(list, e);
        }
        log.info(allList);

    }
    void getChildrens(List<Map<String, Object>> list, Map<String, Object> map) {
        String id = map.get("DISPLAY_COL_ID").toString();
        //查询当前id下的子级的集合
        List<Map<String, Object>> chidrenList = new ArrayList<Map<String, Object>>();
        for (Map<String, Object> e : list) {
            String pid = e.get("PARENT_DISPLAY_COL_ID") == null ? "0" : e.get("PARENT_DISPLAY_COL_ID").toString();
            if(id.equals(pid)){
                chidrenList.add(e);
            }
        }
        //添加子项 子节点
        map.put("chidrens", chidrenList);
        if (chidrenList.size() > 0) {
            for (Map<String, Object> e : chidrenList) {
                //再查一次
                getChildrens(list, e);
            }
        }
    }

}
